/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Control;

import Model.MailUtil;
import Model.User;
import Model.UserDAO;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.mail.MessagingException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.google.code.kaptcha.Constants;
import javax.servlet.RequestDispatcher;

/**
 *
 * @author Nguyen Hung
 */
public class RegisterServlet extends HttpServlet {

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        request.setCharacterEncoding("utf-8");
        HttpSession session = (HttpSession) request.getSession();
        String loginName = request.getParameter("loginName");
        String pass = request.getParameter("pass");
        String email = request.getParameter("email");
        String fullName = request.getParameter("fullName");

        String kaptchaExpected = (String) session.getAttribute(Constants.KAPTCHA_SESSION_KEY);
        String kaptchaReceived = request.getParameter("kaptcha");

        User user = new User(fullName, loginName, pass, email, "register");

        boolean isExit = UserDAO.checkLoginName(user);
        String url = "";
        if (isExit == false && kaptchaExpected.equalsIgnoreCase(kaptchaReceived)) {
            user.save();
            session.setAttribute("user", user);

            MailUtil.sendMail("admin@nhh.com", user.getEmail(), "Thông Báo Đăng Ký Thành Công", "Cáo Ơn Bạn " + user.getUsername() + " đã đăng ký thành viên website máy tính NHH");

            url = "/thanhcong.jsp";

        } else {
            url = "/register.jsp";
            request.setAttribute("notice1", "Lỗi");
        }

        RequestDispatcher dis = getServletContext().getRequestDispatcher(url);
        dis.forward(request, response);
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
